package com.examples.ry.graphics;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FontDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class ListFonts {
	public static void main(String[] args) {
		final Display display = new Display();
		Set s = new HashSet();
		// Add names of all bitmap fonts.
		FontData[] fds = display.getFontList(null, false);
		for (int i = 0; i < fds.length; ++i)
			s.add(fds[i].getName());
		// Add names of all scalable fonts.
		fds = display.getFontList(null, true);
		for (int i = 0; i < fds.length; ++i)
			s.add(fds[i].getName());
		// Sort the result and print it.
		String[] answer = new String[s.size()];
		s.toArray(answer);
		Arrays.sort(answer);
		for (int i = 0; i < answer.length; ++i) {
			System.out.println(answer[i]);
		}

		final Shell shell = new Shell(display, SWT.SHELL_TRIM
				| SWT.NO_REDRAW_RESIZE);

		chooseFont(shell);

		display.dispose();
	}

	public static FontData chooseFont(Shell parent) {
		/**
		 * No matter what font is choosed from dialog box(choosed font will be
		 * saved to file), font of text box will be selected on dialog.
		 */
		FontData fd;
		String fdString = null;
		FontDialog dialog = new FontDialog(parent);
		final String fileName = "the-font";
		// Get FontData string from file if it exists
		try {
			FileReader fr = new FileReader(fileName);
			StringBuffer buf = new StringBuffer();
			for (int ch = fr.read(); ch >= 0; ch = fr.read())
				buf.append((char) ch);
			fr.close();
			fdString = buf.toString();
		} catch (IOException e) {
			e.printStackTrace();
		}
		FontData myTestFontData = null;
		if (fdString != null) {
			Text text = new Text(parent, SWT.MULTI);
			Font textFont = text.getFont();
			myTestFontData = textFont.getFontData()[0];
			System.out.println("TextFont ::" + myTestFontData);
			text.dispose();
			dialog.setFontData(new FontData(fdString));
		}
		fd = dialog.open();
		// If a font was chosen, save the FontData string.
		if (fd != null) {
			FileWriter ofw;
			try {
				ofw = new FileWriter(fileName);
				ofw.write(myTestFontData.toString());
				// ofw.write(fd.toString());
				System.out.println("File written::" + fd.toString());
				ofw.close();
				System.out.println("File written");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		return fd;
	}

	public static FontData[] chooseFontAgain(Shell parent) {
		FontData fd[];
		FontDialog dialog = new FontDialog(parent);
		final String fileName = "the-font-again";
		// Get FontData strings from file if it exists
		ArrayList inputFDs = new ArrayList(10);
		try {
			BufferedReader reader = new BufferedReader(new FileReader(fileName));
			String line = reader.readLine();
			while (line != null) {
				inputFDs.add(new FontData(line));
				line = reader.readLine();
			}
			reader.close();
		} catch (IOException e) {
		}
		if (inputFDs.size() > 0) {
			fd = new FontData[inputFDs.size()];
			inputFDs.toArray(fd);
			dialog.setFontList(fd);
		}
		dialog.open();
		fd = dialog.getFontList();
		// If a font was chosen, save the FontData strings.
		if (fd != null) {
			try {
				BufferedWriter writer = new BufferedWriter(new FileWriter(
						fileName));
				for (int i = 0; i < fd.length; ++i) {
					writer.write(fd[i].toString());
					writer.newLine();
				}
				writer.close();
			} catch (IOException e1) {
			}
		}
		return fd;
	}
}
